package com.aimaoblog.front.service;

import com.aimaoblog.constant.ServerResponse;
import com.aimaoblog.front.api.ArticleFrontDetailService;
import com.aimaoblog.front.dao.ArticleMapper;
import com.aimaoblog.front.dao.CommentMapper;
import com.aimaoblog.front.dao.SortMapper;
import com.aimaoblog.front.dao.TagMapper;
import com.aimaoblog.pojo.Article;
import com.aimaoblog.pojo.Comment;
import com.aimaoblog.vo.ArticleAndTagSortVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author PuaChen
 * @Create 2018-07-06 21:05
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ArticleFrontDetailServiceImpl implements ArticleFrontDetailService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private SortMapper sortMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public ServerResponse<ArticleAndTagSortVo> getArticleDetail(Integer id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null) {
            return ServerResponse.createFailure("获取失败");
        }
        int commentCount = commentMapper.selectArticleCommentCount(article.getId());
        ArticleAndTagSortVo articleAndTagSortVo = new ArticleAndTagSortVo();
        if (StringUtils.isNotBlank(article.getTag())) {
            List<Integer> tagIds = JSONArray.parseArray(article.getTag(), Integer.class);
            if (tagIds != null && !tagIds.isEmpty()) {
                articleAndTagSortVo.setTags(tagMapper.selectAllByIds(tagIds));
            }
        }
        articleAndTagSortVo.setSort(sortMapper.selectByPrimaryKey(article.getRootsort()));
        articleAndTagSortVo.setCommentCount(Long.valueOf(commentCount));
        articleAndTagSortVo.setArticle(article);
        return ServerResponse.createSuccessAndData(articleAndTagSortVo);
    }

    @Override
    public ServerResponse replyComment(Comment comment, Integer parentId) {
        if (parentId == null || comment == null) {
            return ServerResponse.createFailure("参数错误");
        }
        comment.setStatus(false);
        comment.setId(null);
        comment.setParentid(parentId);
        comment.setCreatetime(new Date());
        comment.setReadstate(false);
        int row = commentMapper.insertSelective(comment);
        if (row <= 0) {
            return ServerResponse.createFailure("评论失败");
        }
        return ServerResponse.createSuccess();
    }

    @Override
    public ServerResponse updateArticleReadCount(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        int row = articleMapper.updateReadCount(id);
        if (row <= 0) {
            return ServerResponse.createFailure("更新失败");
        }
        return ServerResponse.createSuccess();
    }
}
